import Api from '@/services/api.js';
import { mapping } from '@/mixin/mixins/mapping';
import EmployeeStatistics from './components/employeeStatistics/index';
export default {
  mixins: [mapping],
  components: {
    EmployeeStatistics
  },
  data() {
    return {
      currType: '', // 当前操作的type值
      department: {}, // 部门
      date: {}, // 时间
      dateArr: [],
      departments: [], // 部门数组
      usersResult: []
    }
  },
  computed: {
    answererInfo() {
      return this.$store.state.answererInfo;
    },
    companyInfo() {
      return this.$store.state.companyInfo;
    },
    privacyData() {
      return this.$store.state.privacyData;
    },
    isHrOrBoss() {
      const roles = this.privacyData.roles;
      return roles.includes('hr') || roles.includes('boss')
    }
  },
  onLoad() {
    if (this.privacyData.openId) {
      this.init()
    } else {
      app.globalData.callBack = () => {
        this.init();
      }
    }
  },
  methods: {
    init() {
      this.getAnalysisResult();
      this.getEnterpriseDepartments();
      this.getDicts();
    },
    async getEnterpriseDepartments() {
      this.departments = await this.$Serv.getEnterpriseDepartments();
    },
    // 获取数据字典
    async getDicts() {
      const ret = await this.$request({
        url: Api.getDicts,
        data: {
          key: this.dictsEnum.recentMonth
        }
      })
      if (ret.code == 200) {
        this.dateArr = ret.result.map(i => {
          i.val = i.title;
          i.key = i.value;
          return i;
        })
      }
    },
    handleTap(e) {
      const { type, value, arr } = e.currentTarget.dataset;
      this.currType = type;
      this.$refs.commonPickerPop.togglePopup(true, { value, arr })
    },
    handleDatePikcer(e) {
      if (this.currType == 'date') {
        this.date = e;
      } else {
        this.department = e;
        this.getAnalysisResult({ departmentId: e.key })
      }
    },
    // 获取分析结果
    async getAnalysisResult(params) {
      const ret = await this.$request({
        url: Api.getAnalysisResult,
        data: {
          enterpriseId: this.companyInfo.enterpriseId,
          ...params
        }
      })
      if (ret.success) {
        this.usersResult = ret.result
        if (this.usersResult.length == 0) return
        for (var i = 0; i < this.usersResult.length; i++) {
          this.usersResult[i].extinforObj = JSON.parse(this.usersResult[i].extinfor)
        }
      }

    }
  },
  async onPullDownRefresh() {
    this.init();
    uni.stopPullDownRefresh();
  }
}